package cn.wubo.task.order.statistics.controller;

import cn.wubo.task.order.statistics.entity.TaskOrderPersonDate;
import cn.wubo.task.order.statistics.record.PersonCalendarRecord;
import cn.wubo.task.order.statistics.record.PersonCalendarResultRecord;
import cn.wubo.task.order.statistics.service.IDocumentService;
import cn.wubo.task.order.statistics.service.ITaskOrderPersonDateService;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * <p>
 * 任务单工时 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2024-11-06
 */
@Controller
@RequestMapping("/api/taskOrderPersonDate")
public class TaskOrderPersonDateController {

    @Resource
    private ITaskOrderPersonDateService taskOrderPersonDateService;

    @Resource
    private IDocumentService documentService;

    @PostMapping("/search")
    public ResponseEntity<List<TaskOrderPersonDate>> search(@RequestBody TaskOrderPersonDate taskOrderPersonDate) {
        LambdaQueryChainWrapper<TaskOrderPersonDate> query = taskOrderPersonDateService.lambdaQuery();
        Optional.ofNullable(taskOrderPersonDate.getTaskOrderId()).ifPresent(str -> query.like(TaskOrderPersonDate::getTaskOrderId, str));
        return ResponseEntity.ok(query.list());
    }

    @GetMapping("/searchPersonNames")
    public ResponseEntity<List<TaskOrderPersonDate>> searchPersonNames() {
        return ResponseEntity.ok(taskOrderPersonDateService.lambdaQuery().select(TaskOrderPersonDate::getPersonName).groupBy(TaskOrderPersonDate::getPersonName).list());
    }

    @PostMapping("/searchPersonCalendar")
    public ResponseEntity<PersonCalendarResultRecord> searchPersonCalendar(@RequestBody PersonCalendarRecord record) {
        return ResponseEntity.ok(taskOrderPersonDateService.searchPersonCalendar(record.year(), record.personNames()));
    }

    @PostMapping("/downloadPersonCalendar")
    public void downloadPersonCalendar(@RequestBody PersonCalendarRecord record, HttpServletResponse response) throws IOException {
        Workbook workbook = documentService.downloadPersonCalendar(record);
        workbook.write(response.getOutputStream());
        workbook.close();
    }

    @PostMapping("/searchPersonMonth")
    public ResponseEntity<PersonCalendarResultRecord> searchPersonMonth(@RequestBody PersonCalendarRecord record) {
        return ResponseEntity.ok(taskOrderPersonDateService.searchPersonMonth(record.year(), record.personNames()));
    }
}
